﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Saker.Kernel.DAL
{
    public class SqlHelper
    {
        public static readonly string connstr =
            ConfigurationManager.ConnectionStrings["SakerConn"].ConnectionString;

        public static int ExecuteNonQuery(string cmdText,
            params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = cmdText;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public static object ExecuteScalar(string cmdText,
            params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = cmdText;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();
                }
            }
        }

        public static DataTable ExecuteDataTable(string cmdText,
            params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = cmdText;
                    cmd.Parameters.AddRange(parameters);
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        adapter.Fill(dt);
                        return dt;
                    }
                }
            }
        }

        public static SqlDataReader ExecuteDataReader(string cmdText,
            params SqlParameter[] parameters)
        {
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = cmdText;
                cmd.Parameters.AddRange(parameters);
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }
        //执行存储过程做增删改
        public static bool ExecuteProc(string ProcName, SqlParameter[] paras)
        {
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = ProcName;
                cmd.Parameters.AddRange(paras);
                cmd.CommandType = CommandType.StoredProcedure;
                int result = cmd.ExecuteNonQuery();
                conn.Close();
                return result > 0;
            }
        }
        /// <summary>
        /// 分页函数
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="tableName">表名</param>
        /// <param name="keyfield">主键</param>
        /// <param name="whereCondition">where</param>
        /// <param name="selectStr">选择字段</param>
        /// <param name="order">排序字段</param>
        /// <param name="orderType">排序,1,降序,0,升序</param>
        /// <returns>DataTable集合</returns>
        public static DataTable GetPagingRecord(int pageIndex, ref int recordCount, int pageSize, ref int pageCount, string tableName, string keyfield,
              string whereCondition, string selectStr, string order, bool orderType)
        {
            using (SqlConnection myConnection = new SqlConnection(connstr))
            {
                //?调用分页
                SqlDataAdapter myCommand = new SqlDataAdapter("GetPagingRecord", myConnection);

                myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
                myCommand.SelectCommand.Parameters.Add(new SqlParameter("@tablename", SqlDbType.NVarChar));
                myCommand.SelectCommand.Parameters["@tablename"].Value = tableName;
                myCommand.SelectCommand.Parameters.Add(new SqlParameter("@fieldlist", SqlDbType.NVarChar));
                myCommand.SelectCommand.Parameters["@fieldlist"].Value = selectStr;

                SqlParameter parameterOrder = new SqlParameter("@orderfield", SqlDbType.NVarChar);
                parameterOrder.Value = order;
                myCommand.SelectCommand.Parameters.Add(parameterOrder);

                SqlParameter parameterOrderType = new SqlParameter("@ordertype", SqlDbType.Bit);
                parameterOrderType.Value = orderType == true ? 0 : 1;
                myCommand.SelectCommand.Parameters.Add(parameterOrderType);

                myCommand.SelectCommand.Parameters.Add(new SqlParameter("@keyfield", SqlDbType.NVarChar));
                myCommand.SelectCommand.Parameters["@keyfield"].Value = keyfield;

                myCommand.SelectCommand.Parameters.Add(new SqlParameter("@pageindex", SqlDbType.Int));
                myCommand.SelectCommand.Parameters["@pageindex"].Value = pageIndex;

                myCommand.SelectCommand.Parameters.Add(new SqlParameter("@pagesize", SqlDbType.Int));
                myCommand.SelectCommand.Parameters["@pagesize"].Value = pageSize;

                myCommand.SelectCommand.Parameters.Add(new SqlParameter("@strwhere", SqlDbType.NVarChar));
                myCommand.SelectCommand.Parameters["@strwhere"].Value = whereCondition;

                myCommand.SelectCommand.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                myCommand.SelectCommand.Parameters["@RecordCount"].Direction = ParameterDirection.Output;

                myCommand.SelectCommand.Parameters.Add(new SqlParameter("@PageCount", SqlDbType.Int));
                myCommand.SelectCommand.Parameters["@PageCount"].Direction = ParameterDirection.Output;

                DataSet ds = new DataSet();
                myCommand.Fill(ds, "pager");
                recordCount = (int)myCommand.SelectCommand.Parameters["@RecordCount"].Value;
                pageCount = (int)myCommand.SelectCommand.Parameters["@PageCount"].Value;
                myConnection.Close();
                return ds.Tables["pager"];
            }
        }
    }
}

